﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/14/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoTipNews
    {
        public daoTipNews()
        {

        }
        
        private TipNews Load(IDataReader de)
        {
            TipNews item = new TipNews();
            item.Description = clsConvert.DbToString("Description", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.ShortDescription = clsConvert.DbToString("ShortDescription", de);
            item.PageTitle = clsConvert.DbToString("PageTitle", de);
            item.MetaKeywords = clsConvert.DbToString("MetaKeywords", de);
            item.MetaDescription = clsConvert.DbToString("MetaDescription", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.TipNewsID = clsConvert.DbToInt("TipNewsID", de);
            item.Title = clsConvert.DbToString("Title", de);
            item.ThumbImage = clsConvert.DbToString("ThumbImage", de);
            item.Type = clsConvert.DbToString("Type", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }

        private TipNews LoadEn(IDataReader de)
        {
            TipNews item = new TipNews();
            item.Description = clsConvert.DbToString("Description", de);
            item.DescriptionEn = clsConvert.DbToString("DescriptionEn", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.ShortDescription = clsConvert.DbToString("ShortDescription", de);
            item.ShortDescriptionEn = clsConvert.DbToString("ShortDescriptionEn", de);
            item.PageTitle = clsConvert.DbToString("PageTitle", de);
            item.MetaKeywords = clsConvert.DbToString("MetaKeywords", de);
            item.MetaDescription = clsConvert.DbToString("MetaDescription", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.TipNewsID = clsConvert.DbToInt("TipNewsID", de);
            item.Title = clsConvert.DbToString("Title", de);
            item.TitleEn = clsConvert.DbToString("TitleEn", de);
            item.ThumbImage = clsConvert.DbToString("ThumbImage", de);
            item.Type = clsConvert.DbToString("Type", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }

        public bool Insert(TipNews objTipNews)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[11];
            try
            {
                para[0] = new SqlParameter(busTipNews.THUMBIMAGE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objTipNews.ThumbImage);
                para[1] = new SqlParameter(busTipNews.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objTipNews.IsActive;
                para[2] = new SqlParameter(busTipNews.TYPE, SqlDbType.NVarChar);
                para[2].Value = objTipNews.Type;
                para[3] = new SqlParameter(busTipNews.TITLE, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objTipNews.Title);
                para[4] = new SqlParameter(busTipNews.SHORTDESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objTipNews.ShortDescription);
                para[5] = new SqlParameter(busTipNews.DESCRIPTION, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objTipNews.Description);
                para[6] = new SqlParameter(busTipNews.CREATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objTipNews.CreatedBy);
                para[7] = new SqlParameter(busTipNews.CREATEDDATE, SqlDbType.DateTime);
                para[7].Value = objTipNews.CreatedDate;
                para[8] = new SqlParameter(busTipNews.PageTitle, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objTipNews.PageTitle);
                para[9] = new SqlParameter(busTipNews.MetaKeywords, SqlDbType.VarChar);
                para[9].Value = clsConvert.StringToDB(objTipNews.MetaKeywords);
                para[10] = new SqlParameter(busTipNews.MetaDescription, SqlDbType.VarChar);
                para[10].Value = clsConvert.StringToDB(objTipNews.MetaDescription);

                result = clsSQL.SPExecute("TipNews_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Insert(TipNews objTipNews)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool InsertEn(TipNews objTipNews)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[14];
            try
            {
                para[0] = new SqlParameter(busTipNews.THUMBIMAGE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objTipNews.ThumbImage);
                para[1] = new SqlParameter(busTipNews.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objTipNews.IsActive;
                para[2] = new SqlParameter(busTipNews.TYPE, SqlDbType.NVarChar);
                para[2].Value = objTipNews.Type;
                para[3] = new SqlParameter(busTipNews.TITLE, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objTipNews.Title);
                para[4] = new SqlParameter(busTipNews.SHORTDESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objTipNews.ShortDescription);
                para[5] = new SqlParameter(busTipNews.DESCRIPTION, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objTipNews.Description);
                para[6] = new SqlParameter(busTipNews.TITLEEN, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objTipNews.TitleEn);
                para[7] = new SqlParameter(busTipNews.SHORTDESCRIPTIONEN, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objTipNews.ShortDescriptionEn);
                para[8] = new SqlParameter(busTipNews.DESCRIPTIONEN, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objTipNews.DescriptionEn);
                para[9] = new SqlParameter(busTipNews.CREATEDBY, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objTipNews.CreatedBy);
                para[10] = new SqlParameter(busTipNews.CREATEDDATE, SqlDbType.DateTime);
                para[10].Value = objTipNews.CreatedDate;
                para[11] = new SqlParameter(busTipNews.PageTitle, SqlDbType.NVarChar);
                para[11].Value = clsConvert.StringToDB(objTipNews.PageTitle);
                para[12] = new SqlParameter(busTipNews.MetaKeywords, SqlDbType.VarChar);
                para[12].Value = clsConvert.StringToDB(objTipNews.MetaKeywords);
                para[13] = new SqlParameter(busTipNews.MetaDescription, SqlDbType.VarChar);
                para[13].Value = clsConvert.StringToDB(objTipNews.MetaDescription);

                result = clsSQL.SPExecute("TipNews_InsertEn", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Insert(TipNews objTipNews)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool DeActive(int tipID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                result = clsSQL.SPExecute("TipNews_DeActive", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "DeActive(int tipID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int tipID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                result = clsSQL.SPExecute("TipNews_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Active(int tipID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int tipID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                result = clsSQL.SPExecute("TipNews_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Delete(int tipID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public TipNews GetByID(int tipID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            TipNews item = new TipNews();
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                using (de = clsSQL.SPDataReader("TipNews_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoTipNews", " GetByID(int tipID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }

        public TipNews GetEnByID(int tipID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            TipNews item = new TipNews();
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                using (de = clsSQL.SPDataReader("TipNews_GetEnByID", para))
                {
                    while (de.Read())
                    {
                        item = LoadEn(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoTipNews", " GetByID(int tipID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }

        public bool Update(TipNews objTipNews)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[12];
            try
            {
                para[0] = new SqlParameter(busTipNews.THUMBIMAGE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objTipNews.ThumbImage);
                para[1] = new SqlParameter(busTipNews.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objTipNews.IsActive;
                para[2] = new SqlParameter(busTipNews.TYPE, SqlDbType.NVarChar);
                para[2].Value = objTipNews.Type;
                para[3] = new SqlParameter(busTipNews.TITLE, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objTipNews.Title);
                para[4] = new SqlParameter(busTipNews.SHORTDESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objTipNews.ShortDescription);
                para[5] = new SqlParameter(busTipNews.DESCRIPTION, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objTipNews.Description);
                para[6] = new SqlParameter(busTipNews.UPDATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objTipNews.UpdatedBy);
                para[7] = new SqlParameter(busTipNews.UPDATEDDATE, SqlDbType.DateTime);
                para[7].Value = objTipNews.UpdatedDate;
                para[8] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[8].Value = objTipNews.TipNewsID;
                para[9] = new SqlParameter(busTipNews.PageTitle, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objTipNews.PageTitle);
                para[10] = new SqlParameter(busTipNews.MetaKeywords, SqlDbType.VarChar);
                para[10].Value = clsConvert.StringToDB(objTipNews.MetaKeywords);
                para[11] = new SqlParameter(busTipNews.MetaDescription, SqlDbType.VarChar);
                para[11].Value = clsConvert.StringToDB(objTipNews.MetaDescription);

                result = clsSQL.SPExecute("TipNews_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Update(TipNews objTipNews)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool UpdateEn(TipNews objTipNews)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[15];
            try
            {
                para[0] = new SqlParameter(busTipNews.THUMBIMAGE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objTipNews.ThumbImage);
                para[1] = new SqlParameter(busTipNews.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objTipNews.IsActive;
                para[2] = new SqlParameter(busTipNews.TYPE, SqlDbType.NVarChar);
                para[2].Value = objTipNews.Type;
                para[3] = new SqlParameter(busTipNews.TITLE, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objTipNews.Title);
                para[4] = new SqlParameter(busTipNews.SHORTDESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objTipNews.ShortDescription);
                para[5] = new SqlParameter(busTipNews.DESCRIPTION, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objTipNews.Description);
                para[6] = new SqlParameter(busTipNews.UPDATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objTipNews.UpdatedBy);
                para[7] = new SqlParameter(busTipNews.UPDATEDDATE, SqlDbType.DateTime);
                para[7].Value = objTipNews.UpdatedDate;
                para[8] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[8].Value = objTipNews.TipNewsID;
                para[9] = new SqlParameter(busTipNews.PageTitle, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objTipNews.PageTitle);
                para[10] = new SqlParameter(busTipNews.MetaKeywords, SqlDbType.VarChar);
                para[10].Value = clsConvert.StringToDB(objTipNews.MetaKeywords);
                para[11] = new SqlParameter(busTipNews.MetaDescription, SqlDbType.VarChar);
                para[11].Value = clsConvert.StringToDB(objTipNews.MetaDescription);
                para[12] = new SqlParameter(busTipNews.TITLEEN, SqlDbType.NVarChar);
                para[12].Value = clsConvert.StringToDB(objTipNews.TitleEn);
                para[13] = new SqlParameter(busTipNews.SHORTDESCRIPTIONEN, SqlDbType.NVarChar);
                para[13].Value = clsConvert.StringToDB(objTipNews.ShortDescriptionEn);
                para[14] = new SqlParameter(busTipNews.DESCRIPTIONEN, SqlDbType.NVarChar);
                para[14].Value = clsConvert.StringToDB(objTipNews.DescriptionEn);

                result = clsSQL.SPExecute("TipNews_UpdateEn", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "Update(TipNews objTipNews)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public TipNewsCollection List(TipNews objTipNews)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            TipNewsCollection items = new TipNewsCollection();

            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objTipNews.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objTipNews.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objTipNews.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = objTipNews.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objTipNews.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("TipNews_List", para);
                while (de.Read())
                {
                    TipNews item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objTipNews.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoTipNews", "List(TipNews objTipNews)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

        public TipNewsCollection ListEn(TipNews objTipNews)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            TipNewsCollection items = new TipNewsCollection();

            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objTipNews.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objTipNews.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objTipNews.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = objTipNews.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objTipNews.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("TipNews_ListEn", para);
                while (de.Read())
                {
                    TipNews item = LoadEn(de);
                    items.Add(item);
                }

                de.Close();
                objTipNews.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoTipNews", "List(TipNews objTipNews)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public bool OrderDown(int tipID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                result = clsSQL.SPExecute("TipNews_OrderDown", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "OrderDown(int tipID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderUp(int tipID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busTipNews.TIPNEWSID, SqlDbType.Int);
                para[0].Value = tipID;
                result = clsSQL.SPExecute("TipNews_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoTipNews", "OrderUp(int tipID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
    }
}

